#!/bin/sh
# Nov 2008 by Nathan Fisher
# Create individual audio tracks from bin/cue
# This program is Free Software

# create working space if first run
[ ! -d "$HOME/.config/cuesplitter/plugins" ] && mkdir -p "$HOME/.config/cuesplitter/plugins"

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"
export APPDIR="$APPDIR"

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null

# version
export VERSION=`cat $APPDIR/version`

# preferences
. $APPDIR/preferences
[ -f "$HOME/.config/cuesplitter/preferences" ] && . "$HOME/.config/cuesplitter/preferences"

# plugins
PLUGINS="<item>$DEFAULT_PLUGIN</item>"
for PLUGIN in `ls $APPDIR/plugins | grep -v $DEFAULT_PLUGIN`
do
 PLUGINS="$PLUGINS<item>$PLUGIN</item>"
done

case $1 in
 -options)
 exec $APPDIR/functions -options
 ;;
 -help)
 exec $APPDIR/functions -help
 ;;
 -about)
 exec $APPDIR/functions -about
 ;;
esac

export CUESPLIT_DLG="
<window title=\"${LOC_MAIN}${VERSION}\" icon_name=\"gtk-cdrom\">
 <vbox>
  <menubar>
   <menu>
    <menuitem icon=\"gtk-preferences\">
     <label>$LOC_OPTIONS</label>
     <action>$APPDIR/functions -options</action>
    </menuitem>
    <separator></separator>
    <menuitem icon=\"gtk-quit\">
     <label>$LOC_QUIT</label>
     <action>EXIT:quit</action>
    </menuitem>
    <label>$LOC_FILE</label>
   </menu>
   <menu>
    <menuitem icon=\"gtk-about\">
     <label>$LOC_ABOUT</label>
     <action>$APPDIR/functions -about &</action>
    </menuitem>
    <menuitem icon=\"gtk-help\">
     <label>$LOC_HELP</label>
     <action>$APPDIR/functions -help &</action>
    </menuitem>
    <label>$LOC_HELP</label>
   </menu>
  </menubar>
  <frame $LOC_INPUT>
   <hbox>
    <text>
     <label>$LOC_CUE</label>
    </text>
    <entry accept=\"file\">
     <variable>CUEFILE</variable>
    </entry>
    <button>
     <input file stock=\"gtk-open\"></input>
     <action type=\"fileselect\">CUEFILE</action>
    </button>
   </hbox>
   <hbox>
    <text>
     <label>$LOC_BIN</label>
    </text>
    <entry accept=\"file\">
     <variable>BINFILE</variable>
    </entry>
    <button>
     <input file stock=\"gtk-open\"></input>
     <action type=\"fileselect\">BINFILE</action>
    </button>
   </hbox>
  </frame>
  <frame $LOC_OUTPUT>
   <hbox>
    <text>
     <label>$LOC_OUTDIR</label>
    </text>
    <entry accept=\"directory\">
     <default>$OUTDIR</default>
     <variable>OUTDIR</variable>
    </entry>
    <button>
     <input file stock=\"gtk-open\"></input>
     <action type=\"fileselect\">OUTDIR</action>
    </button>
   </hbox>
   <hbox>
    <text>
     <label>$LOC_PLUGIN</label>
    </text>
    <combobox editable=\"false\">
     <variable>PLUGIN</variable>
     $PLUGINS
    </combobox>
    <button>
     <input file stock=\"gtk-preferences\"></input>
     <label>$LOC_PLUGIN_OPTS</label>
     <action>. $APPDIR/plugins/\$PLUGIN/plugin.sh && plugin_options</action>
    </button>
   </hbox>
  </frame>
  <hbox>
   <button>
    <input file stock=\"gtk-go-forward\"></input>
    <label>$LOC_GO</label>
    <action>xterm --title \"CueSplitter - $VERSION\" -e $APPDIR/functions -split</action>
   </button>
  </hbox>
 </vbox>
</window>"

gtkdialog3 --program CUESPLIT_DLG
